package com.ylxl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylxl.common.exception.BaseException;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.common.uuid.SnowFlakeUtil;
import com.ylxl.entity.EmployeeLabor;
import com.ylxl.entity.LaborProtection;
import com.ylxl.entity.reponse.EmployeeLaborVO;
import com.ylxl.entity.reponse.LaborProtectionVO;
import com.ylxl.entity.request.*;
import com.ylxl.mapper.EmployeeLaborMapper;
import com.ylxl.mapper.LaborProtectionMapper;
import com.ylxl.service.IEmployeeLaborService;
import com.ylxl.service.ILaborProtectionService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther Gilbert
 * @Date 2023/8/9 14:06
 */
@Service
public class EmployeeLaborServiceImpl  extends ServiceImpl<EmployeeLaborMapper, EmployeeLabor> implements IEmployeeLaborService {

    @Resource
    private EmployeeLaborMapper employeeLaborMapper;
    @Override
    public List<EmployeeLaborVO> selectLabelList(QueryEmployeeLabor queryEmployeeLabor) {
        List<EmployeeLaborVO> laborProtectionVOList = Lists.newArrayList();
        LambdaQueryWrapper<EmployeeLabor> employeeLaborLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLaborLambdaQueryWrapper.eq(StringUtils.isNotEmpty(queryEmployeeLabor.getJobNumber()),EmployeeLabor::getJobNumber,queryEmployeeLabor.getJobNumber())
                .orderByDesc(EmployeeLabor::getCreateTime);
        List<EmployeeLabor> employeeLabors = employeeLaborMapper.selectList(employeeLaborLambdaQueryWrapper);
        BeanUtil.copyProperties(employeeLabors, laborProtectionVOList);
        if(CollectionUtil.isNotEmpty(employeeLabors)){
            employeeLabors.forEach(v->{
                EmployeeLaborVO employeeLaborVO = new EmployeeLaborVO();
                BeanUtil.copyProperties(v, employeeLaborVO);
                laborProtectionVOList.add(employeeLaborVO);
            });
        }
       return laborProtectionVOList;
    }

    @Override
    public int insertLabel(EmployeeLaborCmdOrMdy employeeLaborCmdOrMdy) {
        EmployeeLabor employeeLaborInsert = new EmployeeLabor();
        BeanUtil.copyProperties(employeeLaborCmdOrMdy, employeeLaborInsert);//属性拷贝
        return employeeLaborMapper.insert(employeeLaborInsert);
    }

    @Override
    public int updateLabel(EmployeeLaborCmdOrMdy employeeLaborCmdOrMdy) {
        if(StringUtils.isEmpty(employeeLaborCmdOrMdy.getId())){
            throw new BaseException("劳保id不能为空");
        }
        EmployeeLabor employeeLaborUpdate = new EmployeeLabor();
        BeanUtil.copyProperties(employeeLaborCmdOrMdy, employeeLaborUpdate);//属性拷贝
        return employeeLaborMapper.updateById(employeeLaborUpdate);
    }

    @Override
    public EmployeeLaborVO selctById(Integer id) {
        EmployeeLabor employeeLabor = employeeLaborMapper.selectById(id);
        EmployeeLaborVO employeeLaborVO = new EmployeeLaborVO();
        BeanUtil.copyProperties(employeeLabor, employeeLaborVO);//属性拷贝
        return employeeLaborVO;
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return employeeLaborMapper.deleteBatchIds(ids);
    }
}
